<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, reactive, watch, ref } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { cloneDeep } from 'lodash-es'
import MenuApi from '@/api/menu'
const { required } = useValidator()
const props = defineProps({
  currentRow: {
    type: Object as PropType<any>,
    default: () => null
  }
})

const MformSchema = [
  {
    field: 'icon',
    // field: 'field88',
    label: '菜单图标',
    component: 'IconPicker'
    // value: 'vi-tdesign:archway'
  },
  {
    field: 'path',
    label: '路由地址',
    component: 'Input'
  },
  {
    field: 'visible',
    label: '显示状态',
    component: 'RadioGroup',
    value: '0',
    componentProps: {
      options: [
        {
          label: '显示',
          value: '0'
        },
        {
          label: '隐藏',
          value: '1'
        }
      ],
      on: {
        change: async (val: string) => {
          if (val == '1') {
            FormSchemaData.value[9].hidden = false
          } else {
            FormSchemaData.value[9].hidden = true
          }
        }
      }
    }
  },
  {
    field: 'highPath',
    label: '高亮菜单',
    component: 'Input',
    hidden: props.currentRow.visible == '1' ? false : true
  },
  {
    field: 'isFrame',
    label: '是否外链',
    component: 'RadioGroup',
    value: '1',
    componentProps: {
      options: [
        {
          label: '是',
          value: '0'
        },
        {
          label: '否',
          value: '1'
        }
      ]
    }
  },
  {
    field: 'singlePage',
    label: '是否为单页',
    component: 'RadioGroup',
    value: '0',
    componentProps: {
      options: [
        {
          label: '否',
          value: '0'
        },
        {
          label: '是',
          value: '1'
        }
      ]
    }
  }
]
const CformSchema = [
  {
    field: 'component',
    label: '组件路径',
    component: 'Input'
  },
  {
    field: 'icon',
    // field: 'field88',
    label: '菜单图标',
    component: 'IconPicker',
    hidden: props.currentRow?.singlePage == '1' ? false : true
    // value: 'vi-tdesign:archway'
  },
  {
    field: 'path',
    label: '路由地址',
    component: 'Input'
  },
  {
    field: 'query',
    label: '路由参数',
    component: 'Input'
  },
  {
    field: 'routeName',
    label: '路由名称',
    component: 'Input'
  },
  {
    field: 'perms',
    label: '权限字符',
    component: 'Input'
  },
  {
    field: 'visible',
    label: '显示状态',
    component: 'RadioGroup',
    value: '0',
    componentProps: {
      options: [
        {
          label: '显示',
          value: '0'
        },
        {
          label: '隐藏',
          value: '1'
        }
      ],
      on: {
        change: async (val: string) => {
          // console.log(FormSchemaData.value);
          if (val == '1') {
            FormSchemaData.value[13].hidden = false
            console.log(CformSchema[7])
          } else {
            FormSchemaData.value[13].hidden = true
          }
        }
      }
    }
  },
  {
    field: 'highPath',
    label: '高亮菜单',
    component: 'Input',
    hidden: props.currentRow.visible == '1' ? false : true
  },
  {
    field: 'isFrame',
    label: '是否外链',
    component: 'RadioGroup',
    value: '1',
    componentProps: {
      options: [
        {
          label: '是',
          value: '0'
        },
        {
          label: '否',
          value: '1'
        }
      ]
    }
  }
]
const FformSchema = [
  {
    field: 'perms',
    label: '权限字符',
    component: 'Input'
  }
]
const formSchema = reactive<FormSchema[]>([
  {
    field: 'parentId',
    label: '上级菜单',
    component: 'TreeSelect',
    value: 0,
    colProps: {
      span: 18
    },
    componentProps: {
      nodeKey: 'id',
      props: {
        label: 'label',
        value: 'id',
        children: 'children'
      },
      highlightCurrent: true,
      expandOnClickNode: false,
      checkStrictly: true,
      checkOnClickNode: true,
      clearable: true
    },
    optionApi: async () => {
      const res = await MenuApi.GetDeptListTree()
      const i = [
        {
          children: res.data || [],
          disabled: false,
          id: 0,
          label: '主类目'
        }
      ]
      return i
    }
  },
  {
    field: 'menuType',
    label: '菜单类型',
    component: 'RadioButton',
    colProps: {
      span: 24
    },
    value: 'M',
    componentProps: {
      options: [
        {
          label: '目录',
          value: 'M'
        },
        {
          label: '菜单',
          value: 'C'
        },
        {
          label: '按钮',
          value: 'F'
        }
      ],
      on: {
        change: async (val: string) => {
          // const formData = await getFormData();
          IsHidden(val)
        }
      }
    }
  },
  {
    field: 'menuName',
    label: '菜单名称',
    component: 'Input'
  },
  {
    field: 'orderNum',
    label: '显示顺序',
    component: 'InputNumber'
  },
  {
    field: 'status',
    label: '菜单状态',
    component: 'RadioGroup',
    value: '0',
    componentProps: {
      options: [
        {
          label: '正常',
          value: '0'
        },
        {
          label: '停用',
          value: '1'
        }
      ]
    }
  },
  {
    field: 'isCache',
    label: '是否缓存',
    component: 'RadioGroup',
    value: '0',
    componentProps: {
      options: [
        {
          label: '缓存',
          value: '0'
        },
        {
          label: '不缓存',
          value: '1'
        }
      ]
    }
  }
])
const FormSchemaData = ref<any>([])
const IsHidden = (str: string) => {
  if (str == 'C') {
    FormSchemaData.value = [...formSchema, ...CformSchema]
  } else if (str == 'F') {
    FormSchemaData.value = [...formSchema, ...FformSchema]
  } else {
    FormSchemaData.value = [...formSchema, ...MformSchema]
  }
}
IsHidden(props.currentRow.menuType)
const rules = reactive({
  parentId: [required()],
  path: [required()],
  orderNum: [required()]
})

const { formRegister, formMethods } = useForm()
const { setValues, getFormData, getElFormExpose } = formMethods

const submit = async () => {
  const elForm = await getElFormExpose()
  const valid = await elForm?.validate().catch((err) => {
    console.log(err)
  })
  if (valid) {
    const formData = await getFormData()
    return formData
  }
}

watch(
  () => props.currentRow,
  (value) => {
    if (!value) return
    const currentRow = cloneDeep(value)
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  submit
})
</script>

<template>
  <Form :rules="rules" @register="formRegister" :schema="FormSchemaData" />
  <!-- <IconSelect></IconSelect> -->
</template>

